package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TbBuildingMapper;
import com.hqyj.dao.TbCommunityMapper;
import com.hqyj.entity.SearchBean;
import com.hqyj.pojo.TbHouse;
import com.hqyj.dao.TbHouseMapper;
import com.hqyj.service.TbHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * <p>
 * 房屋表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-26
 */
@Service
public class TbHouseServiceImpl implements TbHouseService {

    @Autowired(required = false)
    TbHouseMapper tbHouseMapper;

    @Autowired(required = false)
    TbCommunityMapper tbCommunityMapper;

    @Autowired
    TbBuildingMapper tbBuildingMapper;

    @Override
    public HashMap<String, Object> list(TbHouse tbHouse) {
        HashMap<String, Object> map = new HashMap<>();

        // 第一步：创建分页对象
        Page<TbHouse> tbHousePage = new Page<>(tbHouse.getPage(), tbHouse.getRow());

        // 创建查询条件对象
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(tbHouse.getId() != null && !tbHouse.getId().equals(""), "id", tbHouse.getId());

        // 第二步：查询分页，获取分页数据集
        Page<TbHouse> tbHouseList = tbHouseMapper.selectPage(tbHousePage, queryWrapper);
        // 获取当前页集合
        map.put("info", tbHouseList.getRecords());
        // 获取总条数
        map.put("total", tbHouseList.getTotal());
        // 获取总页数
        map.put("totalPage", tbHouseList.getPages());

        return map;
    }

    @Override
    public HashMap<String, Object> update(TbHouse tbHouse) {
        HashMap<String ,Object> map = new HashMap<>();
        int num = tbHouseMapper.updateById(tbHouse);
        if (num>0){
            map.put("info","修改成功");
        }else {
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int num = tbHouseMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> listPage(SearchBean searchBean) {
        HashMap<String, Object> map = new HashMap<>();

        // 第一步：创建分页对象
        Page<TbHouse> tbHousePage = new Page<>(searchBean.getCurrentPage(),searchBean.getPageSize());

        // 创建查询条件对象
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(searchBean.getDirection() != null && !searchBean.getDirection().equals(""), "name", searchBean.getDirection());

        // 第二步：查询分页，获取分页数据集
        Page<TbHouse> tbHouseList = tbHouseMapper.selectPage(tbHousePage, queryWrapper);
        // 获取当前页集合
        map.put("info", tbHouseList.getRecords());
        // 获取总条数
        map.put("total", tbHouseList.getTotal());
        // 获取总页数
        map.put("totalPage", tbHouseList.getPages());

        return map;
    }

    @Override
    public HashMap<String, Object> insert(TbHouse tbHouse) {
        HashMap<String ,Object> map = new HashMap<>();
        Integer communityId = tbHouse.getCommunityId();
        Integer buildingId = tbHouse.getBuildingId();
        String communityName = tbCommunityMapper.getNameById(communityId);
        String buildingName = tbBuildingMapper.getNameById(buildingId);
        tbHouse.setCommunityName(communityName);
        tbHouse.setBuildingName(buildingName);

        int num = tbHouseMapper.insert(tbHouse);
        if (num>0){
            map.put("info","添加成功");
        }else {
            map.put("info","添加失败");
        }
        return map;
    }
}
